package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.ProductGroupWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.distinguish.ProductClassfiyContract;
import com.holyn.guofang.presenter.distinguish.ProductClassfiyPresenter;
import com.holyn.guofang.view.BaseFragment;
import com.holyn.guofang.viewcommon.multileveltreelist.Node;
import com.holyn.guofang.viewcommon.popupwindow.PopupSingleSelectFromBottom;
import com.luck.picture.lib.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/7/12.
 */

public class ProductGroupFragment extends BaseFragment implements BaseView, ProductClassfiyContract.GroupView{
    private static final int REQUEST_ACTION_POST_PRODUCT_GROUP_LIST = 0x0001;

    @BindView(R.id.tv_select_type)
    TextView tvSelectType;
    @BindView(R.id.tv_select_group)
    TextView tvSelectGroup;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private ProductClassfiyContract.Presenter presenter;
    private ProductGroupRecyclerAdapter recyclerAdapter;
    private List<Node> nodeList = new ArrayList<>();
    private ProductGroupWo productGroupWo;

    private List<String> typeIdList = null;
    private String curSelectTypeId = "所有";
    private String curSelectGroupId = "所有";


    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_product_group;
    }

    @Override
    protected void initData() {

        presenter = new ProductClassfiyPresenter(this);
        presenter.addGroupView(this);

        presenter.postProductGroupList(REQUEST_ACTION_POST_PRODUCT_GROUP_LIST, "", "", -1, -1);
    }

    @Override
    protected void initView() {
        recyclerAdapter = new ProductGroupRecyclerAdapter(rvList, getActivity(), nodeList);
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
        rvList.setAdapter(recyclerAdapter);
    }


    @OnClick({R.id.tv_select_type, R.id.tv_select_group, R.id.btn_search, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_select_type:
                showTypeIdsSelectPopup();
                break;
            case R.id.tv_select_group:
                if (curSelectTypeId.equals("所有")){
                    toastMsg("请选择类别");
                    return;
                }
                showGroupIdsSelectPopup(curSelectTypeId);
                break;
            case R.id.btn_search:
                recyclerAdapter.notifyAllNodesChange(curSelectTypeId, curSelectGroupId);
                break;
            case R.id.btn_submit:
                String selectGroupId = recyclerAdapter.getSelectGroupId();
                if (!TextUtils.isEmpty(selectGroupId)){
                    Intent intent = getActivity().getIntent();
                    intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE, selectGroupId);
                    intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE_TYPE, ProductClassfiyActivity.TYPE_GROUP_CODE);
                    getActivity().setResult(Activity.RESULT_OK, intent);
                }
                getActivity().finish();
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {

    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {

    }

    @Override
    public void requestComplete(Integer requestAction) {

    }

    @Override
    public void responseSuccess(Integer requestAction, ProductGroupWo productGroupWo) {
        this.productGroupWo = productGroupWo;
        if (productGroupWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            nodeList.clear();
            nodeList.addAll(changeToNodeList(productGroupWo));
            recyclerAdapter.addData(0, nodeList);
        }
    }

    private List<Node> changeToNodeList(ProductGroupWo productGroupWo){
        List<Node> nodeList = new ArrayList<>();
        Map<Integer, String> typeCodeMap = new HashMap<>();
        List<ProductGroupWo.DataBean> dataList = productGroupWo.getData();
        for (int i = 1 ; i <= dataList.size() ; i++){
            ProductGroupWo.DataBean data = dataList.get(i-1);
            String typeCode = data.getTypeCode();

            if (typeCodeMap.containsValue(typeCode)){
                for (Node node : nodeList){
                    if (node.getName().equals("第"+typeCode+"类")){
                        Node node1 = node;
                        Node node2 = new Node(data.getGroupCode()+"", node1.getId(), data.getGroupCode()+" "+data.getName());
                        node2.iconExpand = R.drawable.ic_expand_up;
                        node2.iconNoExpand = R.drawable.ic_expand_down;
                        node2.setParent(node1);
                        node1.getChildren().add(node2);
                        break;
                    }
                }

            } else {
                Node node1 = new Node(typeCode+"", "-1", "第"+typeCode+"类");
                node1.iconExpand = R.drawable.ic_expand_down;
                node1.iconNoExpand = R.drawable.ic_expand_up;
                typeCodeMap.put(Integer.parseInt((String)node1.getId()), typeCode);
                Node node2 = new Node(data.getGroupCode()+"", typeCode, data.getGroupCode()+" "+data.getName());
                node2.iconExpand = R.drawable.ic_expand_down;
                node2.iconNoExpand = R.drawable.ic_expand_up;
                node2.setParent(node1);
                node1.getChildren().add(node2);
                nodeList.add(node1);
            }
//            if (!typeCodeMap.containsValue(typeCode)){
//                Node nodeType = new Node(typeCode+"", "-1", "第"+typeCode+"类");
//                typeCodeMap.put(Integer.parseInt((String)nodeType.getId()), typeCode);
//                nodeList.add(nodeType);
//            }

//            Node nodeGroup = new Node(data.getGroupCode()+"", typeCode+"", data.getGroupCode()+" "+data.getName());
//            nodeList.add(nodeGroup);
        }
        typeCodeMap.clear();
        return nodeList;
    }

    private void showTypeIdsSelectPopup(){
        if (typeIdList == null){
            typeIdList = getTypeIds();
        }

        PopupSingleSelectFromBottom popupSelectTypeIds = new PopupSingleSelectFromBottom(getActivity(), "选择类型编号", typeIdList);
        popupSelectTypeIds.setOnItemClickListener(new PopupSingleSelectFromBottom.OnItemClickListener() {
            @Override
            public void onItemClick(String item, int position) {
                curSelectTypeId = item;
                tvSelectType.setText(curSelectTypeId);
                curSelectGroupId = "所有";
                tvSelectGroup.setText(curSelectGroupId);
            }
        });
        popupSelectTypeIds.showPopupWindow();
    }

    private void showGroupIdsSelectPopup(String typeId){
        List<String> groupIdList = getGroupIds(typeId);
        PopupSingleSelectFromBottom popupSelectGroupIds = new PopupSingleSelectFromBottom(getActivity(), "选择组编号", groupIdList);
        popupSelectGroupIds.setOnItemClickListener(new PopupSingleSelectFromBottom.OnItemClickListener() {
            @Override
            public void onItemClick(String item, int position) {
                curSelectGroupId = item;
                tvSelectGroup.setText(curSelectGroupId);
            }
        });
        popupSelectGroupIds.showPopupWindow();
    }

    private List<String> getTypeIds(){
        List<String> typeIds = new ArrayList<>();
        typeIds.add("所有");
        for (Node node : nodeList){
            typeIds.add(node.getId().toString());
        }
        return typeIds;
    }

    private List<String> getGroupIds(String typeId){
        List<String> groupIds = new ArrayList<>();
        groupIds.add("所有");
        for (Node node : nodeList){
            if (node.getId().equals(typeId)){
                List<Node> nodeChildrenList = node.getChildren();
                for (Node nodeChildren : nodeChildrenList){
                    groupIds.add(nodeChildren.getId().toString());
                }
                break;
            }
        }
        return groupIds;
    }
}
